
package com.hexinfo.dmpro.sparing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.model.ClusterSparingConf;
import com.hexinfo.dmpro.common.utils.ScanCommonConstants;
import com.hexinfo.dmpro.sparing.model.DataSyncConf;
import com.hexinfo.dmpro.sparing.dao.DataSyncConfMapper;
import com.hexinfo.dmpro.sparing.model.ScanMetadata;
import com.hexinfo.dmpro.sparing.model.SyncRuleConf;
import com.hexinfo.dmpro.sparing.service.DataSyncConfService;
import org.springframework.stereotype.Service;

/**
 * 数据同步配置表
 *
 * @author yemw
 * @date 2023-08-30 09:56:26
 */
@Service
public class DataSyncConfServiceImpl extends ServiceImpl<DataSyncConfMapper, DataSyncConf> implements DataSyncConfService {

    @Override
    public Page queryList(Page page, DataSyncConf dataSyncConf) {
        LambdaQueryWrapper<DataSyncConf> wrapper = Wrappers.lambdaQuery();
        wrapper.select().eq(DataSyncConf::getDel,"0");
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return returnPage;
    }

    @Override
    public boolean saveDataSyncConf(SyncRuleConf syncRule, ClusterSparingConf jQCluster, ClusterSparingConf wGCluster, ScanMetadata m) {
        DataSyncConf conf = new DataSyncConf();
        conf.setDataSyncName(m.getTblDatabaseName()+"."+m.getTblName()+ ScanCommonConstants.ClusterName.SJRB.value);
        conf.setSourceCmId(jQCluster.getId());
        conf.setSourceCmFlag(jQCluster.getClusterFlag());
        conf.setSourceBaseName(m.getTblDatabaseName());
        conf.setSourceTableName(m.getTblName());
        conf.setTargetCmId(wGCluster.getId());
        conf.setTargetCmFlag(wGCluster.getClusterFlag());
        conf.setTargetBaseName(m.getTblDatabaseName());
        conf.setTargetTableName(m.getTblName());
        conf.setBatchNo(ScanCommonConstants.ClusterName.ONE.value);
        //同步状态 默认规则为是---已确认
        if("是".equals(syncRule.getIsDefaultRule())){
            conf.setSyncType(ScanCommonConstants.ClusterName.YQR.value);
            // 待确认
        }else if("否".equals(syncRule.getIsDefaultRule())){
            conf.setSyncType(ScanCommonConstants.ClusterName.DQR.value);
        }
        //同步规则配ID
        conf.setSyncRuleId(syncRule.getId());
        conf.setMessageSub(ScanCommonConstants.ClusterName.SJTBPZ.value);
        return this.save(conf);
    }
}
